/*
 * Config
 * 
 * Version information
 * 
 * Feb 17, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.webapp.struts.action;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.isso.service.ISSOService;
import net.amsoft.iservice.isso.webapp.dataObject.LoginData;
import net.amsoft.iservice.isso.webapp.dataObject.SPData;
import net.amsoft.iservice.isso.webapp.struts.form.ConfigForm;
import net.amsoft.iservice.isso.util.ISSOConstants;
import net.amsoft.iservice.util.IServiceUtil;
import net.amsoft.iservice.util.exception.IServiceException;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * This will save receive and save the edited service configuration parameter
 * 
 * @author rahul
 */
public class ConfigureProfile extends BaseAction {
	private static final Logger oLogger = Logger
			.getLogger(ConfigureProfile.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("process() : entry");
		return save(oMapping, oForm, oRequest, oResponse);
	}

	private ActionForward save(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("save() : entry");
		ActionMessages oMsgs = new ActionErrors();
		LoginData oLoginData = (LoginData) oRequest.getSession(false)
				.getAttribute(ISSOConstants.SESSION_LOGIN_OBJ);

		HashMap ohSPData = (HashMap) oRequest.getSession(false).getAttribute(
				ISSOConstants.SESSION_LOGIN_HASH_OBJ);

		String sSpXri = ISSOConstants.getSPXri(oRequest.getServerName());
		SPData oSPData = (SPData) ohSPData.get(sSpXri);
		if (oSPData == null) {
			oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ISSOConstants.ERR_INVALID_REQUEST));
			saveErrors(oRequest, oMsgs);
			oLogger.debug("save() : Data : SPData="+oSPData);
			oLogger.error("save() : Message : Service could not be identified, forward to iname page");
			return oMapping.findForward(ISSOConstants.INAME_FWD);
		}

		ConfigForm oConfigData = (ConfigForm) oForm;
		try {
			ISSOService.activateUpdatePrincipal(oSPData.getGlobalInumber(),
					oConfigData.getFirstName(), oConfigData.getLastName(),
					oConfigData.getGreeting(), oConfigData.getEmailAddress(),
					oConfigData.getNewPassword(), Integer.parseInt(oConfigData
							.getChallengeQuestion()), oConfigData
							.getChallengeAnswer());
			oLogger.info("save(): Message : Principal activated");
		} catch (IServiceException oIServExp) {
			if (oIServExp.getErrorCode() == 2037) {
				oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						ISSOConstants.MSG_ACCOUNT_ALREADY_ACTIVE));
			} else {
				oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						ISSOConstants.MSG_NOT_ABLE_TO_PERFORM_OPERATION));
			}
			saveErrors(oRequest, oMsgs);
			oLogger
					.error("save(): Message : Principal NOT activated, ExceptionCode : "
							+ oIServExp.getErrorCode()
							+ ", ExceptionMessage : " + oIServExp.getMessage());
			return oMapping.getInputForward();
		}

		try {
			// Create cookie(Session and CAll login
			boolean bRememberMachineCookie = false;
			String sSessionCookie = IServiceUtil.generateUniqueID();
			oLoginData.setSessionCookie(sSessionCookie);
			oLoginData.setMachineCookie(IServiceUtil.generateUniqueID());
			setCookies(oResponse, oLoginData.getSessionCookie(), oLoginData
					.getMachineCookie(), bRememberMachineCookie);
			// attempt login
			ISSOService.login(oSPData.getGlobalInumber(), oConfigData
					.getNewPassword(), oRequest.getServerName(), oLoginData
					.getSessionCookie(), oLoginData.getMachineCookie(),
					bRememberMachineCookie);
			oLoginData.setLoginStatus(true);
			oLoginData.setLoggedInSynonym(oConfigData.getLoggedInSynonym());
			oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ISSOConstants.MSG_CONFIG_SAVED));
			saveErrors(oRequest.getSession(false), oMsgs);

			oLogger
					.info("save(): Message : Principal logged in after actiavtion");

			return oMapping.findForward(ISSOConstants.CHKLOGIN_FWD);
		} catch (IServiceException oIServExp) {
			oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ISSOConstants.getError(oIServExp.getErrorCode())));
			saveErrors(oRequest, oMsgs);
			oLogger.error("save(): ExceptionCode : " + oIServExp.getErrorCode()
					+ ", ExceptionMessage : " + oIServExp.getMessage());
			return oMapping.getInputForward();
		}
	}

	private ActionForward cancel(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse) {
		oLogger.info("cancel(): entry");
		ActionMessages oMessages = new ActionErrors();
		oMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
				ISSOConstants.MSG_CONFIG_CANCELED));
		saveErrors(oRequest, oMessages);
		return oMapping.findForward(ISSOConstants.INAME_FWD);
	}
}